<?php


namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Animal;
use App\Models\User;
use Illuminate\Http\Request;
use YangJiSen\CacheUserProvider\CacheForget;

class AnimalController extends BaseController
{
    protected $model = Animal::class;
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $input = $request->input();
        $search = ['parent_category_id' => 0, 'category_id' => 0, 'title' => '', 'user_id' => 0];
        $gender = $request->input('gender');
        $users = Animal::apply($search, $input)->when($gender, function ($q) use($gender) {
            $q->where('gender', $gender);
        })->orderBy("id", "desc")
            ->paginate(per_page());
        return $this->paginate($users);
    }

    public function _after_delete($animal) {
        $user = User::find($animal->user_id);
        $user->animal_count -= 1;
        if ($user->animal_count >= 0) {
            $user->save();
            CacheForget::CacheForget($user);
        }
    }
}

